let app = getApp();
var siteinfo = require('../../siteinfo.js');
Page({
  data: {
    list: [], //放置返回数据的数组
    no_data: false, //“没有数据”的变量，默认false，隐藏
    reachLoading: true, //"上拉加载"的变量，默认false，隐藏
    current: 1, //第几页,第几次加载
    number: 10, //需要返回数据的个数
  },
  onLoad: function(options) {},

  onShow: function() {
    this.getUnreadNum()
    this.getNewsList()
  },
  //未读消息条数
  getUnreadNum: function() {
    var unreadNum = wx.getStorageSync('unreadNum')
    if (unreadNum != '' && unreadNum != 0) {
      wx.setTabBarBadge({
        index: 2,
        text: unreadNum
      })
    } else {
      wx.removeTabBarBadge({
        index: 2
      })
    }
  },
  //复制链接
  copy: function(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.url,
      success(res) {
        wx.getClipboardData({
          success(res) {}
        })
      }
    })
  },
  //直接跳转
  toView: function(e) {
    let url = e.currentTarget.dataset.url;
    //获取最后一个.的位置
    var index = url.lastIndexOf(".");
    //获取后缀
    var ext = url.substr(index + 1);
    if (ext == 'doc' || ext == 'docx' || ext == 'xls' || ext == 'xlsx' || ext == 'ppt' || ext == 'pptx' || ext == 'pdf') {
      wx.downloadFile({
        url: url,
        header: {},
        success: function(res) {
          var filePath = res.tempFilePath;
         // console.log(filePath);
          wx.openDocument({
            filePath: filePath,
            fileType: ext.toLowerCase(),
            success: function(res) {
              // console.log('打开文档成功')
            }
          })
        },
        fail: function(res) {
          // console.log('文件下载失败');
        },
        complete: function(res) {},
      })
    } else {
      wx.navigateTo({
        url: '/pages/dataLibrary/webView?url=' + url,
      })
    }
  },

  //获取信息列表
  getNewsList: function() {
    var _this = this
    _this.setData({
      current: 1,
      no_data: false
    });
    app._get(siteinfo.selNewsList, {
      firstColumn: '证书考试门户',
      columnName: '资料文库',
      number: _this.data.number,
      current: _this.data.current
    }, function(result) {
      var res = result.data.records
      for (var i = 0; i < res.length; i++) {
        res[i].createrDate = res[i].createrDate.substring(0, 10)
      }
      _this.setData({
        list: res
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.stopPullDownRefresh();
    })
  },
  onPullDownRefresh: function() {
    this.getNewsList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.searchScrollLower();
  },
  searchScrollLower: function() {
    let _this = this;
    if (_this.data.reachLoading && !_this.data.no_data) {
      _this.setData({
        current: _this.data.current + 1, //每次触发上拉事件，把current+1
      });
      wx.showLoading({
        title: '加载中',
      })
      //网络请求
      _this.reachAppendMsgList();
    }
  },
  /**
   * 上拉追加消息列表
   */
  reachAppendMsgList: function() {
    var _this = this;
    var url = null
    app._get(siteinfo.selNewsList, {
      firstColumn: '证书考试门户',
      columnName: '资料文库',
      number: _this.data.number,
      current: _this.data.current
    }, function(result) {
      //以/n把字符串进行拆分
      var list = result.data.records;
      for (var i = 0; i < res.length; i++) {
        list[i].createrDate = list[i].createrDate.substring(0, 10)
      }
      _this.setData({
        list: _this.data.list.concat(result.data.records)
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.hideLoading();
    });
  },
  /**
   * 返回顶部
   */
  goTop: function(e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 显示/隐藏 返回顶部按钮
   */
  onPageScroll: function(e) {
    this.setData({
      floorstatus: e.scrollTop > 200
    })
  },

  onShareAppMessage: function() {

  }
})